package com.sun.personal.juc;

import java.util.concurrent.*;

/**
 * @author syh
 * @date 2021/7/8 9:26
 */
public class MyThreadPoolDemo {

    public static void main(String[] args) {

        int core = Runtime.getRuntime().availableProcessors();

        System.out.println(core);

        int i1 = core << 1;
        System.out.println(i1);

        ExecutorService executorService = new ThreadPoolExecutor(2, 5, 2L, TimeUnit.SECONDS, new LinkedBlockingQueue(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        try {

//            for (int i = 0; i < 9; i++) {
//                //execute开启线程提交不带有返回值的任务
//                executorService.execute(()->{
//                    System.out.println(Thread.currentThread().getName() + "执行任务");
//                });
//            }


            for (int i = 0; i < 10; i++) {
                Future<Integer> future = executorService.submit(() -> {
                    System.out.println(Thread.currentThread().getName() + "执行任务");
                    return 1024;
                });
                Integer integer = future.get();
                System.out.println("结果为：" + integer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown(); //关闭线程池
        }
    }
}

